package model

import "ginblog/api/common/encrypt"

type Admin struct {
	BaseModel
	AdminName  string `gorm:"type:varchar(20);not null;comment:'名称'" json:"admin_name"`
	PassWord   string `gorm:"type:varchar(32);not null;comment:'密码'" json:"pass_word"`
	Email      string `gorm:"type:varchar(32);not null;comment:'邮箱'" json:"email"`
	Phone      string `gorm:"type:varchar(11);not null;comment:'手机号' " json:"phone"`
	BindBlogId int    `gorm:"type:int(11);default 0;comment:'绑定博客Id'" json:"bind_blog_id"`
}

func (A *Admin) ExaminePassWord(Password string) bool {
	return A.PassWord == Password
}

func (A *Admin) PassWordEncrypt(PassWord string) string {
	return encrypt.UseScrypt(PassWord)
}

func (A *Admin) GetUserName() string {
	return A.AdminName
}

func (A *Admin) GetUserId() uint {
	return A.ID
}

func (A *Admin) UpdateInfo() bool {
	if A.PassWord != "" {
		A.PassWord = A.PassWordEncrypt(A.PassWord)
	}
	err := Db.Model(&A).Where("id  = ? ", A.ID).Omit("Id").Updates(A).Error
	if err != nil {
		return false
	}
	return true
}
